﻿namespace QuickMail.BusinessLogic
{
    using System;
    using HigLabo.Net.Imap;
    using HigLabo.Net.Pop3;
    using HigLabo.Net.Smtp;

    using QuickMail.DataModels;

    static class TestConnectionWorker
    {
        #region Fields

        private static bool IncomingTestResult;
        private static bool OutgoingTestResult;

        #endregion //Fields

        #region Test connection methods

        /// <summary>
        /// Tests connection by IMAP protocol.
        /// </summary>
        public static bool ImapTestConnection(ImapClient imapClient)
        {
            try
            {
                IncomingTestResult = false;
                if (imapClient.Authenticate())
                {
                    imapClient.Close();
                    return IncomingTestResult = true;
                }

                return IncomingTestResult = false;
            }
            catch (Exception ex)
            {
                StaticFields.Logger.Error(" -- {0}.{1} -- {2}", typeof(EnvironmentWorker).FullName,
                    System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message);
                return IncomingTestResult = false;
            }
        }

        /// <summary>
        /// Tests connection by POP3 protocol.
        /// </summary>
        public static bool Pop3TestConnection(Pop3Client pop3Client)
        {
            try
            {
                IncomingTestResult = false;
                if (pop3Client.Authenticate())
                {
                    pop3Client.Close();
                    return IncomingTestResult = true;
                }

                return IncomingTestResult = false;
            }
            catch (Exception ex)
            {
                StaticFields.Logger.Error(" -- {0}.{1} -- {2}", typeof(EnvironmentWorker).FullName,
                    System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message);
                return IncomingTestResult = false;
            }

        }

        /// <summary>
        /// Tests connection by SMTP protocol.
        /// </summary>
        public static bool SmtpTestConnection(SmtpClient smtpClient)
        {
            try
            {
                OutgoingTestResult = false;
                if (smtpClient.Authenticate())
                {
                    smtpClient.Close();
                    return OutgoingTestResult = true;
                }

                return OutgoingTestResult = false;
            }
            catch (Exception ex)
            {
                StaticFields.Logger.Error(" -- {0}.{1} -- {2}", typeof(EnvironmentWorker).FullName,
                    System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message);
                return OutgoingTestResult = false;
            }
        }

        #endregion //Test connection methods
    }
}
